package com.lan.controller.sys;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lan.SysConstants;
import com.lan.businessObject.MapBo;
import com.lan.controller.BaseController;
import com.lan.entity.sys.Role;
import com.lan.entity.sys.User;
import com.lan.enums.sys.user.ResultEnum;
import com.lan.service.sys.RoleService;
import com.lan.service.sys.UserService;
import com.lan.viewObject.Result;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  用户 前端控制器
 * </p>
 *
 * 列表页面增加 type 属性, 指角色(code)
 * 也就是说 获取的是 特定角色的用户列表
 * 当 type 为 0 时, 获取的是所有觉得的用户
 *
 * @author shadow
 * @since 2022-06-04
 */
@Controller
@RequestMapping("/user")
@RequiresPermissions("user:list")
@RequiresRoles(SysConstants.SUPER_ADMIN)
public class UserController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    /**
     * 首页.
     *
     * */
    @GetMapping("/index")
    public String index() {
        return "sys/user/index";
    }

    /**
     * 列表 页.
     *
     * */
    @GetMapping("/list")
    public String list() {
        return "sys/user/list";
    }

    /**
     * 详情 页.
     *
     * */
    @GetMapping("/detail/{id}")
    public String detail(ModelMap model, @PathVariable("id") final String id) {
        model.addAttribute(SysConstants.USER, userService.getById(id));
        return "sys/user/detail";
    }

    /**
     * 新增 页.
     *
     * */
    @GetMapping("/add")
    @RequiresPermissions("user:add")
    public String add(final ModelMap model) {
        model.addAttribute(SysConstants.USER, new User());
        return "sys/user/add";
    }

    /**
     * 编辑 页.
     *
     * */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("user:edit")
    public String edit(final ModelMap model, @PathVariable("id") final String id) {
        model.addAttribute(SysConstants.USER, userService.getById(id));
        return "sys/user/edit";
    }

    /**
     * 配置 页.
     *
     * */
    @GetMapping("/config/{id}")
    @RequiresPermissions("user:edit")
    public String config(final ModelMap model, @PathVariable("id") final String id) {
        List<Role> roleList = roleService.getListByWrapper(new Role());
        List<Role> roleListByUserId = roleService.getRoleListByUserId(id);
//        roleList.forEach(x ->
//            roleListByUserId.forEach(y -> {
//            if (StringUtils.equals(x.getValue(), y.getCode())) {
//                x.setChecked(Boolean.TRUE);
//            }
//        }));
        model.addAttribute("roleList", roleList);
        model.addAttribute("id", id);
        return "sys/user/config";
    }

    /**
     * 列表.
     *
     * */
    @ResponseBody
    @PostMapping("/list/{type}")
    public Result getList(@PathVariable("type") final String type, final User user) {
        List<User> list;
        if ("0".equals(type))
            list = userService.getListByWrapper(user);
        else
            list = userService.getListBySQL(type, user);
        return Result.ok()
                .put(Result.CODE, 0)
                .put(Result.STATUS, 200)
                .put(Result.DATA, list)
                .put(Result.COUNT, list.size())
                .put(Result.MSG, "");
    }

    /**
     * 配置用户角色.
     *
     * */
    @ResponseBody
    @PostMapping("/configRole/{id}/{role}")
    @RequiresPermissions("user:edit")
    public Result configRole(@PathVariable("id") final String id, @PathVariable("role") final String roles) {
        if (id.isEmpty() || roles.isEmpty()) {
            go(Boolean.FALSE);
        }
        int result = roleService.setUserRole(id, Arrays.asList(roles.split(SysConstants.COMMA)));
        return go(result == 1);
    }

    /**
     * 删除.
     *
     * */
    @ResponseBody
    @PostMapping("/del/{id}")
    @RequiresPermissions("user:del")
    public Result del(@PathVariable("id") final String id) {
        int result = userService.deleteById(id);
        return go(result == 1);
    }

    private Result go(boolean result) {
        if (result) {
            return Result.ok();
        }
        return Result.error("失败");
    }

}
